"""
    Register Flask Command
    :Author: MiaLi
"""
import click

from messageboard import app, db
from messageboard.models import Message


@app.cli.command()
@click.option('--drop', is_flag=True, help='drop all and then create all')
def init_db(drop):
    """Initialize the database."""
    if drop:
        click.confirm('This operation will delete the database, do you want to continue?', abort=True)
        db.drop_all()
        click.echo('Drop tables ok.')
    db.create_all()
    click.echo('Initialized database ok.')


@app.cli.command()
@click.option('--count', default=20, help='Quantity of messages, default is 20.')
def forge(count):
    """Create messages list"""
    from faker import Faker

    fake = Faker(locale='zh_CN')
    click.echo('Create messages record.')

    for i in range(count):
        message = Message(name=fake.name(), body=fake.text(max_nb_chars=80),
                          timestamp=fake.date_time_this_year())
        db.session.add(message)

    db.session.commit()
    click.echo(f'Create {count} messages OK.')
